package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.UniversityAgencyFeeDto;
import xyz.zhouzhaodong.crm.entity.UniversityAgencyFee;
import xyz.zhouzhaodong.crm.mapper.UniversityAgencyFeeMapper;
import xyz.zhouzhaodong.crm.mapper.UniversityMapper;
import xyz.zhouzhaodong.crm.service.IUniversityAgencyFeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 高校-中介费 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-22
 */
@Service
public class UniversityAgencyFeeServiceImpl extends ServiceImpl<UniversityAgencyFeeMapper, UniversityAgencyFee> implements IUniversityAgencyFeeService {

    @Resource
    UniversityAgencyFeeMapper universityAgencyFeeMapper;

    @Resource
    UniversityMapper universityMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addUniversityAgencyFee(UniversityAgencyFeeDto universityAgencyFeeDto) {
        try {
            UniversityAgencyFee universityAgencyFee = dtoChange(universityAgencyFeeDto);
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            universityAgencyFee.setId(id);
            universityAgencyFee.setCreateUser(universityAgencyFeeDto.getStaffId());
            universityAgencyFee.setCreateTime(new Date());
            universityAgencyFeeMapper.insert(universityAgencyFee);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteUniversityAgencyFee(UniversityAgencyFeeDto universityAgencyFeeDto) {
        try {
            UniversityAgencyFee universityAgencyFee = new UniversityAgencyFee();
            universityAgencyFee.setId(universityAgencyFeeDto.getId());
            universityAgencyFee.setUpdateUser(universityAgencyFeeDto.getStaffId());
            universityAgencyFee.setUpdateTime(new Date());
            universityAgencyFee.setFlag(1);
            universityAgencyFeeMapper.updateById(universityAgencyFee);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateUniversityAgencyFee(UniversityAgencyFeeDto universityAgencyFeeDto) {
        try {
            UniversityAgencyFee universityAgencyFee = dtoChange(universityAgencyFeeDto);
            universityAgencyFee.setUpdateUser(universityAgencyFeeDto.getStaffId());
            universityAgencyFee.setUpdateTime(new Date());
            universityAgencyFeeMapper.updateById(universityAgencyFee);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectUniversityAgencyFee(UniversityAgencyFeeDto universityAgencyFeeDto) {
        Integer flag = universityAgencyFeeDto.getFlag() == null ? 0 : universityAgencyFeeDto.getFlag();
        if (universityAgencyFeeDto.getPageNo() != 0 && universityAgencyFeeDto.getPageSize() != 0){
            Page<UniversityAgencyFeeDto> page = new Page<>(universityAgencyFeeDto.getPageNo(), universityAgencyFeeDto.getPageSize());
            universityAgencyFeeDto.setFlag(flag);
            IPage<UniversityAgencyFeeDto> list = universityAgencyFeeMapper.selectListPage(page, universityAgencyFeeDto);
            for(UniversityAgencyFeeDto u : list.getRecords()){
                u.setUniversity(universityMapper.selectById(u.getUniversityId()));
            }
            return RestMessage.success(list);
        } else {
            QueryWrapper<UniversityAgencyFee> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("university_id", universityAgencyFeeDto.getUniversityId());
            queryWrapper.eq("flag", flag);
            List<UniversityAgencyFee> list = universityAgencyFeeMapper.selectList(queryWrapper);
            return RestMessage.success(list);
        }
    }

    private UniversityAgencyFee dtoChange(UniversityAgencyFeeDto universityAgencyFeeDto) {
        UniversityAgencyFee universityAgencyFee = new UniversityAgencyFee();
        universityAgencyFee.setId(universityAgencyFeeDto.getId());
        universityAgencyFee.setUniversityId(universityAgencyFeeDto.getUniversityId());
        universityAgencyFee.setYear(universityAgencyFeeDto.getYear());
        universityAgencyFee.setCollectionDate(universityAgencyFeeDto.getCollectionDate());
        universityAgencyFee.setMoney(universityAgencyFeeDto.getMoney());
        universityAgencyFee.setUnit(universityAgencyFeeDto.getUnit());
        universityAgencyFee.setInvoiceStatus(universityAgencyFeeDto.getInvoiceStatus());
        universityAgencyFee.setInvoiceMail(universityAgencyFeeDto.getInvoiceMail());
        universityAgencyFee.setContactTeacher(universityAgencyFeeDto.getContactTeacher());
        universityAgencyFee.setTelephone(universityAgencyFeeDto.getTelephone());
        universityAgencyFee.setRemark(universityAgencyFeeDto.getRemark());
        universityAgencyFee.setFlag(universityAgencyFeeDto.getFlag());
        return universityAgencyFee;
    }

}
